package net.zjitc.service.impl;

import net.zjitc.context.LocalThreadHolder;
import net.zjitc.mapper.BookRssHistoryMapper;
import net.zjitc.pojo.api.ApiResult;
import net.zjitc.pojo.api.Result;
import net.zjitc.pojo.dto.query.extend.BookRssHistoryQueryDto;
import net.zjitc.pojo.entity.BookRssHistory;
import net.zjitc.pojo.vo.BookRssHistoryVO;
import net.zjitc.service.BookRssHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

//书籍订阅业务逻辑实现
@Service
public class BookRssHistoryServiceImpl implements BookRssHistoryService {

    @Resource
    private BookRssHistoryMapper bookRssHistoryMapper;

    //书籍订阅新增
    @Override
    public Result<Void> save(BookRssHistory bookRssHistory) {
        BookRssHistoryQueryDto dto = new BookRssHistoryQueryDto();
        dto.setUserId(LocalThreadHolder.getUserId());
        dto.setBookId(bookRssHistory.getBookId());
        List<BookRssHistoryVO> bookRssHistoryVOS = bookRssHistoryMapper.query(dto);
        if (!CollectionUtils.isEmpty(bookRssHistoryVOS)) {
            return ApiResult.error("该本书已经订阅，请勿重复操作");
        }
        bookRssHistory.setCreateTime(LocalDateTime.now());
        bookRssHistory.setUserId(LocalThreadHolder.getUserId());
        bookRssHistoryMapper.save(bookRssHistory);
        return ApiResult.success();
    }

    //书籍订阅删除
    @Override
    public Result<Void> batchDelete(List<Integer> ids) {
        bookRssHistoryMapper.batchDelete(ids);
        return ApiResult.success();
    }

    //书籍订阅修改
    @Override
    public Result<Void> update(BookRssHistory bookRssHistory) {
        bookRssHistoryMapper.update(bookRssHistory);
        return ApiResult.success();
    }

    //书籍订阅查询
    @Override
    public Result<List<BookRssHistoryVO>> query(BookRssHistoryQueryDto dto) {
        List<BookRssHistoryVO> bookRssHistoryVOS = bookRssHistoryMapper.query(dto);
        Integer totalCount = bookRssHistoryMapper.queryCount(dto);
        return ApiResult.success(bookRssHistoryVOS, totalCount);
    }

}
